using System;
using System.Collections.Generic;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Updating;
using DomainComponents.Common;
using DomainComponents.XCRM;

namespace XCRM.Module {
	public sealed partial class XCRMModule : ModuleBase {
		public XCRMModule() {
			InitializeComponent();
		}

        public override void Setup(XafApplication application) {
            base.Setup(application);
            QualifyLeadParameters.QualifyLeadParametersType = typeof(QualifyLeadParametersWithOpportunity);
            QualifyLeadParameters.IContactObjectType = typeof(ICRMContact);
            QualifyLeadParameters.IAccountObjectType = typeof(ICRMAccount);
            QualifyLeadParametersWithOpportunity.IOpportunityObjectType = typeof(ICRMOpportunity);

            RegisterEntities();
        }

        public override void Setup(ApplicationModulesManager moduleManager) {
            base.Setup(moduleManager);
        }

        public static void RegisterEntities() {
            XafTypesInfo.Instance.RegisterEntity("Opportunity", typeof(ICRMOpportunity));
            XafTypesInfo.Instance.RegisterEntity("Lead", typeof(ICRMLead));
            XafTypesInfo.Instance.RegisterEntity("Order", typeof(ICRMOrder));
            XafTypesInfo.Instance.RegisterEntity("Quote", typeof(ICRMQuote));
            XafTypesInfo.Instance.RegisterEntity("Invoice", typeof(ICRMInvoice));
            XafTypesInfo.Instance.RegisterEntity("Contact", typeof(ICRMContact));
            XafTypesInfo.Instance.RegisterEntity("Account", typeof(ICRMAccount));
            XafTypesInfo.Instance.RegisterEntity("Product", typeof(ICRMProduct));
            XafTypesInfo.Instance.RegisterEntity("SaleItem", typeof(ISaleItem));
            XafTypesInfo.Instance.RegisterEntity("User", typeof(IMyAppUser));
            XafTypesInfo.Instance.RegisterEntity("Role", typeof(IPersistentRole));
            XafTypesInfo.Instance.RegisterEntity("Address", typeof(IGenericAddress));
            //XafTypesInfo.Instance.RegisterEntity("Phone", typeof(IPhone)); TODO
            XafTypesInfo.Instance.RegisterEntity("ObjectAccessPermission", typeof(IObjectAccessPermission));
            XafTypesInfo.Instance.RegisterEntity("Note", typeof(INote));
            XafTypesInfo.Instance.RegisterEntity("PersistentFileData", typeof(IPersistentFileData));
            XafTypesInfo.Instance.RegisterEntity("PersistentResource", typeof(IPersistentResource));
            XafTypesInfo.Instance.RegisterEntity("Event", typeof(ICRMActivity));
            XafTypesInfo.Instance.RegisterEntity("Category", typeof(ICategory));

            XafTypesInfo.Instance.RegisterEntity("LeadToSalesAnalytic", typeof(ILeadHistoryRecord));

            XafTypesInfo.Instance.RegisterSharedPart(typeof(INotes));
            XafTypesInfo.Instance.RegisterSharedPart(typeof(IActivityTarget));
            XafTypesInfo.Instance.RegisterSharedPart(typeof(IPerson));
            XafTypesInfo.Instance.RegisterSharedPart(typeof(IQuoteCustomer));
            XafTypesInfo.Instance.RegisterSharedPart(typeof(IOrderCustomer));
            XafTypesInfo.Instance.RegisterSharedPart(typeof(IOpportunityCustomer));
            XafTypesInfo.Instance.RegisterSharedPart(typeof(ICustomer));
            XafTypesInfo.Instance.RegisterSharedPart(typeof(ISaleBase));
        }

        public override IEnumerable<ModuleUpdater> GetModuleUpdaters(IObjectSpace objectSpace, Version versionFromDB) {
            ModuleUpdater updater = new Updater(objectSpace, versionFromDB);
            return new ModuleUpdater[] { updater };
        }
	}
}
